package com.dq.service.messsge.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.domain.message.oneself.Message;
import com.dq.domain.message.oneself.NoticeMessage;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.messsge.NoticeMessageService;
import com.dq.service.sys.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author cf
 *
 * 岗位逻辑
 */
@Service
public class NoticeMessageServiceImpl extends BaseRepositories implements NoticeMessageService {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 添加， 批量
     * @param noticeMessage
     * @return
     * @throws AdminException
     */
    @Override
    public void saveList(NoticeMessage noticeMessage, List<Long> caseUserIds, Message.MessageType messageType)  throws AdminException {
        if(caseUserIds.isEmpty()) {
            throw new AdminException("消息接收者不能为空");
        }

        for (Long caseUserId : caseUserIds) {
            SysUser sysUser = dao.get(SysUser.class, caseUserId);
            noticeMessage.setCaseUser(sysUser);
            NoticeMessage save = save(noticeMessage, messageType);
        }
    }
    /**
     * 添加
     * @param noticeMessage
     * @return
     * @throws AdminException
     */
    @Override
    public NoticeMessage save(NoticeMessage noticeMessage, Message.MessageType messageType)  throws AdminException {
        if(noticeMessage.getCaseUser() == null) {
            throw new AdminException("请选择消息接收者");
        }
        if(messageType == null) {
            throw new  AdminException("messageType为空");
        }

        noticeMessage.setMessageStatus(NoticeMessage.MessageStatus.noSee);
        noticeMessage.setMessageType(messageType);

        noticeMessage.setVersion(Constant.VERSION_VALUE);
        noticeMessage.setDeleted(false);
        noticeMessage.setCreateTime(System.currentTimeMillis());
        noticeMessage.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        noticeMessage.setCreateUser(user);
        noticeMessage.setUpdateUser(user);

        noticeMessage.setOrg(noticeMessage.getCaseUser().getSection());

        // 接收者（员工/顾客）
        if(noticeMessage.getUserType() == null) {
            throw new AdminException("消息接收者类型为空");
        }


        dao.saveOrModify(noticeMessage);

        return noticeMessage;
    }

    /**
     *修改
     * @param noticeMessage
     * @return
     * @throws AdminException
     */
    @Override
    public NoticeMessage update(NoticeMessage noticeMessage) throws AdminException {
        noticeMessage.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            noticeMessage.setUpdateUser(user);
        }

        dao.saveOrModify(noticeMessage);

        return noticeMessage;
    }

}

